from django.db import models
from goods.models import SPU,SKU
# Create your models here.

# 商品SPU规格
# spu 外键  '商品SPU'
# name varchar(20)  "规格名称"
class SPUSpecification(models.Model):
    """商品SPU规格"""
    spu = models.ForeignKey(SPU, on_delete=models.CASCADE, related_name='specs', verbose_name='商品SPU')
    name = models.CharField(max_length=20, verbose_name='规格名称')

    class Meta:
        db_table = 'spu_specification'
        verbose_name_plural = '商品SPU规格'

    def __str__(self):
        return '%s: %s' % (self.spu.name, self.name)




# 规格选项
# value varchar(20)  '选项值'
# spec 外键（SPUSpecification） '规格'
class SpecificationOption(models.Model):
    """规格选项"""
    spec = models.ForeignKey(SPUSpecification, related_name='options', on_delete=models.CASCADE, verbose_name='规格')
    value = models.CharField(max_length=20, verbose_name='选项值')

    class Meta:
        db_table = 'specification_option'
        verbose_name_plural = '规格选项'

    def __str__(self):
        return '%s - %s' % (self.spec, self.value)


# sku的规格
# sku  外键（Good）
# spec 外键 （SPUSpecification）
# option 外键（SpecificationOption）
class SKUSpecification(models.Model):
    """SKU具体规格"""
    sku = models.ForeignKey(SKU, related_name='specs', on_delete=models.CASCADE, verbose_name='sku')
    spec = models.ForeignKey(SPUSpecification, on_delete=models.PROTECT, verbose_name='规格名称')
    option = models.ForeignKey(SpecificationOption, on_delete=models.PROTECT, verbose_name='规格值')

    class Meta:
        db_table = 'sku_specification'
        verbose_name_plural = 'SKU规格'

    def __str__(self):
        return '%s: %s - %s' % (self.sku.name, self.spec.name, self.option.value)


# SKU图片
# sku, 商品sku， 引用Good
# image  varchar(300) '商品图片'
class SKUImage(models.Model):
    """SKU图片"""
    sku = models.ForeignKey(SKU, on_delete=models.CASCADE, verbose_name='sku')
    image = models.CharField(verbose_name='商品图片地址', max_length=300)

    class Meta:
        db_table = 'sku_image'
        verbose_name_plural = 'SKU图片'

    def __str__(self):
        return '%s %s' % (self.sku.name, self.id)


